package com.spc.car.data.service.org.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spc.car.data.constant.OrganizationConstants;
import com.spc.car.data.domain.org.vo.QueryOrgByOrgNameVO;
import com.spc.car.data.entity.org.Org;
import com.spc.car.data.mapper.org.OrgMapper;
import com.spc.car.data.domain.org.po.GetOrgListPO;
import com.spc.car.data.service.org.OrgService;
import com.spc.car.data.domain.org.vo.ListOrgVO;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 机构表 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-03-25
 */
@Service
public class OrgServiceImpl extends ServiceImpl<OrgMapper, Org> implements OrgService {

    @Override
    public String uniqueOrganizationCode(String code, Integer id) {
        Org info = getOne(new QueryWrapper<Org>()
                .eq("code",code)
                .ne(null != id,"id", id)
        );
        if (ObjectUtils.isNotNull(info)){
            return OrganizationConstants.NOT_UNIQUE;
        }
        return OrganizationConstants.UNIQUE;
    }

    @Override
    public int updateOrganization(Org org) {
        return baseMapper.updateById(org);
    }

    @Override
    public Page<ListOrgVO> selectOrgList(GetOrgListPO<ListOrgVO> po) {
        Page<ListOrgVO> page = po.getPage();
        Long star = page.getSize() * (page.getCurrent()-1L);
        List<ListOrgVO> listOrgVOS = baseMapper.inquireListOrg(page.getSize(), po, star);
        page.setRecords(listOrgVOS);
        page.setTotal(baseMapper.inquireListOrgCount(po));
        return page;
    }

    @Override
    public List<QueryOrgByOrgNameVO> queryOrgByOrgName(String orgName) {
        return baseMapper.queryOrgByOrgName(orgName);
    }
    
    
}
